/**
 * 
 */
package com.cea.papyrus.diagram.component;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteSeparator;
import org.eclipse.gef.palette.PaletteStack;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.ui.palette.FlyoutPaletteComposite.FlyoutPreferences;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.uml2.uml.Abstraction;
import org.eclipse.uml2.uml.Artifact;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.Component;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.Generalization;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Realization;
import org.eclipse.uml2.uml.Usage;

import com.cea.papyrus.core.IPapyrusUIConstants;
import com.cea.papyrus.core.PapyrusMessages;
import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.CreationEntryFactory;
import com.cea.papyrus.core.editor.DiagramPalette;
import com.cea.papyrus.core.pseudoelement.GraphicalLink;
import com.cea.papyrus.core.pseudoelement.InstanceSpecificationLink;
import com.cea.papyrus.core.ui.dialogs.PapyrusMessageDialog;

/**
 * Manages creation of the palette in the component diagram editor.
 * 
 * @author al090761
 *
 */
public final class ComponentDiagramPaletteFactory extends CreationEntryFactory {
	/**
	 * The Constant PALETTE_DOCK_LOCATION.
	 */
	private static final String PALETTE_DOCK_LOCATION = "ShapesEditorPaletteFactory.Location";

	/**
	 * The Constant PALETTE_SIZE.
	 */
	private static final String PALETTE_SIZE = "ShapesEditorPaletteFactory.Size";

	/**
	 * The Constant PALETTE_STATE.
	 */
	private static final String PALETTE_STATE = "ShapesEditorPaletteFactory.State";

	/**
	 * Creates the shapes drawer.
	 * 
	 * @return the palette container
	 */
	private static PaletteContainer createShapesDrawer() {
		PaletteDrawer componentsDrawer = new PaletteDrawer("Uml Element");

		// Begin of entry stack for Package
		List<PaletteEntry> entries = new ArrayList<PaletteEntry>();
		try {
			///////////////////////////////////////////////////////////////////
			// Begin of entry stack for Package, model and profile creation
			///////////////////////////////////////////////////////////////////
			PaletteStack paletteStack = new PaletteStack("", "", null);	
			
			// Package creation
			paletteStack.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddPackageCommand_Label, 
					PapyrusMessages.AddPackageCommand_Description, 
					Package.class,
					null));			
			entries.add(paletteStack);
			
			// End of entry stack for Package creation	
			
			///////////////////////////////////////////////////////////////////
			// Begin of entry stack for Component creation	
			///////////////////////////////////////////////////////////////////
			paletteStack = new PaletteStack("", "", null);
			
			// Action in palette Component creation
			paletteStack.add(PapyrusPlugin.creationToolFactory
					.getCombinedTemplateCreationToolEntry(
							PapyrusMessages.AddComponentCommand_Label,
							PapyrusMessages.AddComponentCommand_Description,
							Component.class, null));
			
			entries.add(paletteStack);
			// End of entry stack for Component creation
			
			///////////////////////////////////////////////////////////////////
			// Begin of entry stack for  Artifact creation	
			///////////////////////////////////////////////////////////////////
			paletteStack = new PaletteStack("", "", null);				

			// Artifact creation
			paletteStack.add(PapyrusPlugin.creationToolFactory
					.getCombinedTemplateCreationToolEntry(
							PapyrusMessages.AddArtifactCommand_Label,
							PapyrusMessages.AddArtifactCommand_Description,
							Artifact.class, null));				
			
			entries.add(paletteStack);
			// End of palette stack for Artifact
			
			///////////////////////////////////////////////////////////////////
			// Begin of entry stack for  Port creation	
			///////////////////////////////////////////////////////////////////
			paletteStack = new PaletteStack("", "", null);				

			// Port creation
			paletteStack.add(PapyrusPlugin.creationToolFactory
					.getCombinedTemplateCreationToolEntry(
							PapyrusMessages.AddPortCommand_Label,
							PapyrusMessages.AddPortCommand_Description,
							Port.class, null));	

			

			// End of palette stack for Port, Connector creation
			entries.add(paletteStack);
			// End of palette stack for Port
			
			///////////////////////////////////////////////////////////////////
			// Begin of entry stack for Class and Interface creation	
			///////////////////////////////////////////////////////////////////
			paletteStack = new PaletteStack("", "", null);

			//Action in palette for Class creation
			paletteStack.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddClassCommand_Label,
					PapyrusMessages.AddClassCommand_Description,
					Class.class,
					null));
			//Action in palette for Instance Specification creation
			paletteStack.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddClassCommand_Label,
					PapyrusMessages.AddClassCommand_Description,
					InstanceSpecification.class,
					null));
			
			//Action in palette for Interface creation
			// Not yet operational
			// Requires management of different presentations for Interface
//			paletteStack.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
//					PapyrusMessages.AddInterfaceCommand_Label,
//					PapyrusMessages.AddInterfaceCommand_Description,
//					Interface.class,
//					null));
			
			entries.add(paletteStack);

			// End of palette stack for Class, Interface creation
			
			///////////////////////////////////////////////////////////////////
			// Begin of entry stack for Property, Operation, etc.	
			///////////////////////////////////////////////////////////////////
			paletteStack = new PaletteStack("", "", null);

			// Property creation
			paletteStack.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddPropertyCommand_Label,
					PapyrusMessages.AddPropertyCommand_Description,
					Property.class,
					null));
			
			// Operation creation
			paletteStack.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddOperationCommand_Label,
					PapyrusMessages.AddOperationCommand_Description,
							Operation.class,
							null));
			
			entries.add(paletteStack);
		
			
			///////////////////////////////////////////////////////////////////
			// Begin of entry stack for DataType, Enumeration and PrimitiveType
			///////////////////////////////////////////////////////////////////
			paletteStack = new PaletteStack("", "", null);

			// Primitive type creation
			paletteStack.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddPrimitiveTypeCommand_Label,
					PapyrusMessages.AddPrimitiveTypeCommand_Description,
					PrimitiveType.class,
					null));
			
			// Datatype creation
			paletteStack.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddDataTypeCommand_Label,
					PapyrusMessages.AddDataTypeCommand_Description,
					DataType.class,
					null));
			
			// Enumeration creation
			paletteStack.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddEnumerationCommand_Label,
					PapyrusMessages.AddEnumerationCommand_Description,
					Enumeration.class,
					null));
			entries.add(paletteStack);		

			// End of palette stack for PrimitiveType, DataType and Enummeration 

			///////////////////////////////////////////////////////////////////
			// Comment creation
			///////////////////////////////////////////////////////////////////	
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddCommentCommand_Label,
					PapyrusMessages.AddCommentCommand_Description,
					Comment.class,
					null));
			
			///////////////////////////////////////////////////////////////////
			// Constraint creation
			///////////////////////////////////////////////////////////////////	
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddConstraintCommand_Label,
					PapyrusMessages.AddConstraintCommand_Description,
					Constraint.class,
					null));
		} catch (Exception e) {
			PapyrusMessageDialog.display(e.getMessage(), e);
		}

		componentsDrawer.addAll(entries);

		return componentsDrawer;
	}

	/**
	 * Creates the PaletteRoot and adds all palette elements. Use this factory
	 * method to create a new palette for your graphical editor.
	 * 
	 * @return a new PaletteRoot
	 */
	public static DiagramPalette createPalette() {
		DiagramPalette palette = new DiagramPalette(IPapyrusUIConstants.COMPONENT_DIAGRAM);
		palette.add(createToolsGroup(palette));
		palette.add(createShapesDrawer());

		// Create additionnal menu defined in extensions
		palette.createMenuExtensions();

		return palette;
	}
	/**
	 * Return a FlyoutPreferences instance used to save/load the preferences of
	 * a flyout palette.
	 * 
	 * @return the flyout preferences
	 */
	static FlyoutPreferences createPalettePreferences() {
		return new FlyoutPreferences() {
			private IPreferenceStore getPreferenceStore() {
				return Activator.getDefault().getPreferenceStore();
			}

			public int getDockLocation() {
				return getPreferenceStore().getInt(PALETTE_DOCK_LOCATION);
			}

			public int getPaletteState() {
				return getPreferenceStore().getInt(PALETTE_STATE);
			}

			public int getPaletteWidth() {
				return getPreferenceStore().getInt(PALETTE_SIZE);
			}

			public void setDockLocation(int location) {
				getPreferenceStore().setValue(PALETTE_DOCK_LOCATION, location);
			}

			public void setPaletteState(int state) {
				getPreferenceStore().setValue(PALETTE_STATE, state);
			}

			public void setPaletteWidth(int width) {
				getPreferenceStore().setValue(PALETTE_SIZE, width);
			}
		};
	}
	/**
	 * Creates the tools group.
	 * 
	 * @param palette the palette
	 * 
	 * @return the palette container
	 */
	private static PaletteContainer createToolsGroup(PaletteRoot palette) {
		// PaletteDrawer componentsDrawer = new PaletteDrawer("RelationShips");
		PaletteGroup toolGroup = new PaletteGroup("RelationShips");

		// Add a selection tool to the group
		ToolEntry tool = new PanningSelectionToolEntry();
		toolGroup.add(tool);
		palette.setDefaultEntry(tool);

		// Add a marquee tool to the group
		toolGroup.add(new MarqueeToolEntry());

		// Add a (unnamed) separator to the group
		toolGroup.add(new PaletteSeparator());	
		
		// list of entries to be added in the palette container
		List <PaletteEntry> entries = new ArrayList<PaletteEntry>();
		
		try {			
			///////////////////////////////////////////////////////////////////
			// Begin of entry stack for Dependencies
			///////////////////////////////////////////////////////////////////
			PaletteStack paletteStack = new PaletteStack("", "", null);
			
			// Add Dependency
			paletteStack.add(PapyrusPlugin.creationToolFactory
					.getConnectionCreationToolEntry(
							PapyrusMessages.AddDependencyCommand_Label,
							PapyrusMessages.AddDependencyCommand_Description,
							Dependency.class, null));

			// Add Realization connection tool
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddRealizationCommand_Label
					, PapyrusMessages.AddRealizationCommand_Description
					, Realization.class
					, null));
			
			// Add Usage connection tool
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddUsageCommand_Label
					, PapyrusMessages.AddUsageCommand_Description
					, Usage.class
					, null));

			// Add Abstraction connection tool
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddAbstractionCommand_Label,
					PapyrusMessages.AddAbstractionCommand_Description,
					Abstraction.class,
					null));
			
			entries.add(paletteStack);
			
			// Connector creation
//			paletteStack.add(PapyrusPlugin.creationToolFactory
//					.getCombinedTemplateCreationToolEntry(
//							PapyrusMessages.AddConnectorCommand_Label,
//							PapyrusMessages.AddConnectorCommand_Description,
//							Connector.class, null));
			///////////////////////////////////////////////////////////////////
			// Begin of entry stack for Generalizations
			///////////////////////////////////////////////////////////////////
			paletteStack = new PaletteStack("", "", null);
			
			// Add Generalization
			entries.add(PapyrusPlugin.creationToolFactory
							.getConnectionCreationToolEntry(
									PapyrusMessages.AddGeneralizationCommand_Label,
									PapyrusMessages.AddGeneralizationCommand_Description,
									Generalization.class, null));
			entries.add(paletteStack);
			// Generalization palette stack - end
			
			// Association palette stack
			paletteStack = new PaletteStack("", "", null);			
			// Add Association
			paletteStack.add(PapyrusPlugin.creationToolFactory
					.getConnectionCreationToolEntry(
							PapyrusMessages.AddAssociationCommand_Label,
							PapyrusMessages.AddAssociationCommand_Description,
							Association.class, null));			
			
			// Add InterfaceRealization connection tool	
			// Not yet implemented (requires management of different presentations for interface
//			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
//					PapyrusMessages.AddInterfaceRealizationCommand_Label
//					, PapyrusMessages.AddInterfaceRealizationCommand_Description
//					, InterfaceRealization.class
//					, null));
			
			// Add link
			paletteStack.add(PapyrusPlugin.creationToolFactory
					.getConnectionCreationToolEntry(
							PapyrusMessages.AddLinkCommand_Label,
							PapyrusMessages.AddLinkCommand_Description,
							InstanceSpecificationLink.class, null));		
			entries.add(paletteStack);
			// Association palette stack - end

			///////////////////////////////////////////////////////////////////
			// Begin of tool for Comment and constraint links
			///////////////////////////////////////////////////////////////////
			
			// Add GraphicalLink
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddGraphicalLinkCommand_Label,
					PapyrusMessages.AddGraphicalLinkCommand_Description,
					GraphicalLink.class,
					null));	

		} catch (Exception e) {
			PapyrusMessageDialog.display(e.getMessage(), e);
		}

		toolGroup.addAll(entries);
		return toolGroup;
	}
}
